﻿using gl_SMT.Model;
using Microsoft.Office.Interop.Excel;
using Microsoft.Win32;
using SMTUploadProductionData.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace gl_SMT.View
{
    /// <summary>
    /// hl_ControlDataSelect.xaml 的交互逻辑
    /// </summary>
    public partial class hl_DataSelect : UserControl
    {
        public hl_DataSelect()
        {
            InitializeComponent();
            this.Loaded += Hl_ControlDataSelect_Loaded;
        }

        public string MachineName { get; set; }
        DatabaseReadWrite database = new DatabaseReadWrite();
        private void Hl_ControlDataSelect_Loaded(object sender, RoutedEventArgs e)
        {
            dp_SatrtDate.Text = DateTime.Now.ToString();
            dp_EndDate.Text = DateTime.Now.ToString();
            SelectData();
        }

        private void btn_Select_Click(object sender, RoutedEventArgs e)
        {
            SelectData();
        }

        private void SelectData()
        {
            List<hl_model> hl_Models = new List<hl_model>();
            database.SelectData(ref hl_Models, "MachineName", MachineName.Split(' ')[1], dp_SatrtDate.Text, dp_EndDate.Text);
            dataGrid.ItemsSource = hl_Models;
            txt_SelectNumber.Text = hl_Models.Count.ToString();
        }

        private void btn_Export_Click(object sender, RoutedEventArgs e)
        {
            List<hl_model> hl_Models = new List<hl_model>();
            database.SelectData(ref hl_Models, "MachineName", MachineName.Split(' ')[1], dp_SatrtDate.Text, dp_EndDate.Text);
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "Excel Worksheets|*.xls";
            if (saveFile.ShowDialog() == true)
            {
                Application excelApp = new Application();
                Workbook workbook = excelApp.Workbooks.Add();
                Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header.ToString();// 设置表头
                }
                for (int i = 0; i < hl_Models.Count; i++)
                {
                    worksheet.Cells[2 + i, 1] = hl_Models[i].MachineName;
                    worksheet.Cells[2 + i, 2] = hl_Models[i].logTime;
                    worksheet.Cells[2 + i, 3] = hl_Models[i].PowerSupply;
                    worksheet.Cells[2 + i, 4] = hl_Models[i].Machine;
                    worksheet.Cells[2 + i, 5] = hl_Models[i].HotAirMotor;
                    worksheet.Cells[2 + i, 6] = hl_Models[i].TransportSpeed;
                    worksheet.Cells[2 + i, 7] = hl_Models[i].TrackAlarm;
                    worksheet.Cells[2 + i, 8] = hl_Models[i].UpperFurnaceChamber;
                    worksheet.Cells[2 + i, 9] = hl_Models[i].EmergencyBrake;
                    worksheet.Cells[2 + i, 10] = hl_Models[i].UpperChannel1;
                    worksheet.Cells[2 + i, 11] = hl_Models[i].LowerPassage1;
                    worksheet.Cells[2 + i, 12] = hl_Models[i].UpperChannel2;
                    worksheet.Cells[2 + i, 13] = hl_Models[i].LowerPassage2;
                    worksheet.Cells[2 + i, 14] = hl_Models[i].UpperChannel3;
                    worksheet.Cells[2 + i, 15] = hl_Models[i].LowerPassage3;
                    worksheet.Cells[2 + i, 16] = hl_Models[i].UpperChannel4;
                    worksheet.Cells[2 + i, 17] = hl_Models[i].LowerPassage4;
                    worksheet.Cells[2 + i, 18] = hl_Models[i].UpperChannel5;
                    worksheet.Cells[2 + i, 19] = hl_Models[i].LowerPassage5;
                    worksheet.Cells[2 + i, 20] = hl_Models[i].UpperChannel6;
                    worksheet.Cells[2 + i, 21] = hl_Models[i].LowerPassage6;
                    worksheet.Cells[2 + i, 22] = hl_Models[i].UpperChannel7;
                    worksheet.Cells[2 + i, 23] = hl_Models[i].LowerPassage7;
                    worksheet.Cells[2 + i, 24] = hl_Models[i].UpperChannel8;
                    worksheet.Cells[2 + i, 25] = hl_Models[i].LowerPassage8;
                    worksheet.Cells[2 + i, 26] = hl_Models[i].UpperChannel9;
                    worksheet.Cells[2 + i, 27] = hl_Models[i].LowerPassage9;
                    worksheet.Cells[2 + i, 28] = hl_Models[i].UpperChannel10;
                    worksheet.Cells[2 + i, 29] = hl_Models[i].LowerPassage10;
                    worksheet.Cells[2 + i, 30] = hl_Models[i].Fan1;
                    worksheet.Cells[2 + i, 31] = hl_Models[i].Fan2;
                    worksheet.Cells[2 + i, 32] = hl_Models[i].ChainSpeedSetting;
                    worksheet.Cells[2 + i, 33] = hl_Models[i].OxygenConcentration;
                }
                // 自动调整列宽
                worksheet.Columns.AutoFit();

                // 保存文件
                workbook.SaveAs(saveFile.FileName);
                workbook.Close();
                excelApp.Quit();

                // 释放 COM 对象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                MessageBox.Show("导出完成");
            }
        }
    }
}
